﻿using System;
using System.Xml.Linq;

using BodaNetCommunications.Nodes;

namespace BodaNetCommunications.ModbusTcpComs
{
    public class ModbusNode : BodaNodeClass
    {
        //
        // 摘要:
        //     ModbusRTU客户端
        public const int ModbusRTU = 1000;

        //
        // 摘要:
        //     ModbusTCP客户端
        public const int ModbusTCP = 2000;

        //
        // 摘要:
        //     ModbusASCII客户端
        public const int ModbusASCII = 3000;

        //
        // 摘要:
        //     ModbusUDP客户端
        public const int ModbusUDP = 4000;

        //
        // 摘要:
        //     设备的类别
        public int ModbusType { get; set; }

        //
        // 摘要:
        //     安装的时间
        public DateTime InstallationDate { get; set; }

        //
        // 摘要:
        //     连接超时的时间，单位毫秒
        public int ConnectTimeOut { get; set; }

        //
        // 摘要:
        //     服务器的创建日期
        public DateTime CreateTime { get; set; }

        //
        // 摘要:
        //     该Modbus设备是否激活
        public bool IsActive { get; set; }

        public ModbusNode()
        {
            NodeType = 2;
            NodeHead = "ModbusNode";
            CreateTime = DateTime.Now;
            ConnectTimeOut = 2000;
            InstallationDate = DateTime.Now;
            IsActive = true;
        }

        public override void LoadByXmlElement(XElement element)
        {
            base.LoadByXmlElement(element);
            ModbusType = int.Parse(element.Attribute("ModbusType").Value);
            ConnectTimeOut = int.Parse(element.Attribute("ConnectTimeOut").Value);
            CreateTime = DateTime.Parse(element.Attribute("CreateTime").Value);
            InstallationDate = DateTime.Parse(element.Attribute("InstallationDate").Value);
            IsActive = bool.Parse(element.Attribute("IsActive").Value);
        }

        public override XElement ToXmlElement()
        {
            XElement xElement = base.ToXmlElement();
            xElement.SetAttributeValue("ModbusType", ModbusType);
            xElement.SetAttributeValue("ConnectTimeOut", ConnectTimeOut);
            xElement.SetAttributeValue("CreateTime", CreateTime.ToString());
            xElement.SetAttributeValue("InstallationDate", InstallationDate.ToString());
            xElement.SetAttributeValue("IsActive", IsActive.ToString());
            return xElement;
        }
    }
}